import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import axios from 'axios';
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import '@fontsource/inter/400.css' // 常规字重
import '@fontsource/inter/500.css' // 中等字重
import '@fontsource/inter/600.css' // 半粗体
import '@fontsource/inter/700.css' // 粗体


axios.defaults.baseURL = 'https://sss.ljhrlx.xyz'; // 后端 API 基地址
// axios.defaults.baseURL = 'http://localhost:8084'; // 后端 API 基地址

// 恢复 token 和 adminInfo
const storedToken = sessionStorage.getItem('token');
const storedAdminInfo = sessionStorage.getItem('adminInfo');

if (storedToken) {
    store.commit('setToken', storedToken);  // 恢复 token
}

if (storedAdminInfo) {
    store.commit('setAdminInfo', JSON.parse(storedAdminInfo));  // 恢复 adminInfo
}

const app = createApp(App);
app.use(router);
app.use(store);

// 路由守卫：禁用缓存，并检查登录状态
router.beforeEach((to, from, next) => {
    const token = store.state.token || sessionStorage.getItem('token');
    if (!token && to.path !== '/') {
        next('/'); // 如果没有 token，重定向到登录页
    } else {
        next();
    }

    // 禁用缓存，确保每次访问时都重新加载页面
    if (to.meta.noCache) {
        document.body.setAttribute('data-no-cache', true);
    }
});
// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
// 注册 ElementPlus
app.use(ElementPlus)
app.mount('#app');
